package com.bolt.core.view.tag;


import com.bolt.common.utils.StrUtil;
import com.bolt.core.view.components.ComponentsBase;
import com.bolt.core.view.components.Form;
import com.bolt.core.view.components.PageView;

import javax.servlet.jsp.JspException;


public class FormTemplateTag extends AbstractClosingTag {

    private boolean updateState = false;
    private String beanName;
    private String path;
    private String saveAction;
    private String updateAction;
    private String entity;
    private boolean haveSubmitBtn = true;


    public String getSaveAction() {
        return saveAction;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getPath() {
        return path;
    }

    public void setSaveAction(String saveAction) {
        this.saveAction = saveAction;
    }

    public String getUpdateAction() {
        return updateAction;
    }

    public void setUpdateAction(String updateAction) {
        this.updateAction = updateAction;
    }

    public String getEntity() {
        return entity;
    }

    public void setEntity(String entity) {
        this.entity = entity;
    }

    public boolean isHaveSubmitBtn() {
        return haveSubmitBtn;
    }

    public void setHaveSubmitBtn(boolean haveSubmitBtn) {
        this.haveSubmitBtn = haveSubmitBtn;
    }


    public String getBeanName() {
        return beanName;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public boolean isUpdateState() {
        return updateState;
    }

    public void setUpdateState(boolean updateState) {
        this.updateState = updateState;
    }

    @Override
    public ComponentsBase getUI() {
        PageView page = getPageRepository().findPage(path);
        Form form = page.getForm(id);
        return form;
    }

    @Override
    public void evaluateExpressions() throws JspException {
        super.evaluateExpressions();
        Object entityBean = null;
        if (StrUtil.isNotEmpty(beanName)) {
            entityBean = EvalHelper.eval("beanName", getBeanName(), this, pageContext);
            getRequest().setAttribute(Form.PAGECONTEXT_FORMVALUES, entityBean);
        }
        String string = null;
        if ((string = EvalHelper.evalString("path", getPath(), this, pageContext)) != null) {
            setPath(string);
        }

        if ( getRequest().getAttribute(Form.PAGECONTEXT_FORMVALUES) != null) {
            setUpdateState(true);
            getRequest().setAttribute(Form.FORM_UPDATESTATE,updateState);
        } else {
            setUpdateState(false);
            getRequest().setAttribute(Form.FORM_UPDATESTATE,updateState);
        }
        getRequest().setAttribute(Form.FORM_ACTION_URL,updateState?getForm().getUpdateAction():getForm().getSaveAction());

    }

    @Override
    public void populateParams() {
        super.populateParams();
        Form form = getForm();
        if (StrUtil.isNotBlank(this.getEntity())) {
            form.setEntity(this.getEntity());
        }
        if (StrUtil.isNotBlank(this.getSaveAction())) {
            form.setSaveAction(this.getSaveAction());
        }
        if (StrUtil.isNotBlank(this.getUpdateAction())) {
            form.setUpdateAction(this.getUpdateAction());
        }
        form.setHaveSubmitBtn(this.isHaveSubmitBtn());
    }

    public Form getForm() {
        return (Form) component;

    }

}
